package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.WebDriver;
import util.PropertyLoader;
import util.ReportWriter;

import java.util.concurrent.TimeUnit;

public class OnlineOrderPage extends BasePage
{
    private static final String URL = PropertyLoader.loadProperty("site.url");
    private static final String URL_CHECKOUT_PAGE ="http://my.rozetka.com.ua/checkout/";
    private static final String PRODUCT ="Razer Ferox 2013 (RZ05-00500200-R3G2)";

    private static final By SEARCH_FIELD = By.className("header-search-input-text");
    private static final By SUBMIT_SEARCH_BUTTON = By.className("btn-link-i");

    private static final By PURCHASE_BUTTON =By.name("topurchasesfromcatalog");
    private static final By REPURCHASE_BLUE_BUTTON =By.className("ibutton-css-link-blue");

    private static final By POPUP_CHECKOUT_BUTTON =By.xpath("//*[@id=\"popup-checkout\"]/span/span/span");
    private static final By CART_ORDER_LINK =By.name("open_cart");

    private static final By NEW_CLIENT_NAME_FIELD =By.name("user[recipient_title]");
    private static final By NEW_CLIENT_CITY_FIELD =By.xpath("//*[@id=\"suggest_locality\"]");
    private static final By NEW_CLIENT_PHONE_FIELD =By.cssSelector("#reciever_phone");
    private static final By NEW_CLIENT_MAIL_FIELD =By.id("reciever_email");

    private static final By PROCEED_TO_ORDER_CONFIRMATION_PAGE_BUTTON =By.className("btn-link-i");
    private static final By ORDER_CONFIRMATION_BUTTON =By.xpath("//*[@id=\"make-order\"]");
    private static final By SUGGESTION_CITY =By.xpath("//*[@id=\"step_contacts\"]/div/div[1]/div[1]/div[2]/div[2]/div/div/div/ul/li[1]");




    public OnlineOrderPage(WebDriver driver) {
        super(driver);
    }

    public void openOnlineOrderPage()
    {
        driver.get(URL);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }

    public void searchProduct()
    {
        driver.findElement(SEARCH_FIELD).clear();
        driver.findElement(SEARCH_FIELD).sendKeys(PRODUCT);
        driver.findElement(SUBMIT_SEARCH_BUTTON).click();
    }

    public void onlineOrder()
    {
        if (driver.findElements(PURCHASE_BUTTON).size()>0)
        {
            driver.findElement(PURCHASE_BUTTON).click();
        } else
            driver.findElement(REPURCHASE_BLUE_BUTTON).click();

        if( driver.findElements(POPUP_CHECKOUT_BUTTON).size()>0)
        {
            driver.findElement(POPUP_CHECKOUT_BUTTON).click();
        } else
            driver.findElement(CART_ORDER_LINK).click();
    }
    public boolean productIsOrdered()
    {
        return driver.getCurrentUrl().equals(URL_CHECKOUT_PAGE);
    }

    //********************Negative online order**************************************

     public void checkNewClientData(String name, String city, String phone,String mail)
     {
         driver.get(URL_CHECKOUT_PAGE);

         driver.findElement(NEW_CLIENT_NAME_FIELD).clear();
         driver.findElement(NEW_CLIENT_NAME_FIELD).sendKeys(name);

         driver.findElement(NEW_CLIENT_CITY_FIELD).sendKeys(city);
         driver.findElement(SUGGESTION_CITY).click();

         driver.findElement(NEW_CLIENT_PHONE_FIELD).clear();
         driver.findElement(NEW_CLIENT_PHONE_FIELD).sendKeys(phone);

         driver.findElement(NEW_CLIENT_MAIL_FIELD).clear();
         driver.findElement(NEW_CLIENT_MAIL_FIELD).sendKeys(mail);

         try{ driver.findElement(PROCEED_TO_ORDER_CONFIRMATION_PAGE_BUTTON).click(); }
         catch (ElementNotVisibleException e){ReportWriter.error("ElementNotVisibleException");}
     }
     public boolean isNewClientDataCorrect()
     {
         return driver.findElements(ORDER_CONFIRMATION_BUTTON).size()>0;
     }
}
